package cn.tedu.datamanager.controller;

import cn.tedu.datamanager.pojo.dto.AdminAddNewDTO;
import cn.tedu.datamanager.pojo.dto.AdminLoginDTO;
import cn.tedu.datamanager.pojo.dto.UserAddNewDTO;
import cn.tedu.datamanager.pojo.dto.UserLoginDTO;
import cn.tedu.datamanager.pojo.vo.AdminListItemVO;
import cn.tedu.datamanager.pojo.vo.LevelListItemVO;
import cn.tedu.datamanager.pojo.vo.UserLevelListItemVO;
import cn.tedu.datamanager.pojo.vo.UserListItemVO;
import cn.tedu.datamanager.serve.IAdminService;
import cn.tedu.datamanager.serve.IUserService;
import cn.tedu.datamanager.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "04. 客户管理模块")
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    IUserService userService;

    public UserController() {
        log.info("创建控制器对象：AdminController");
    }


    // http://localhost:9081/users
    @ApiOperation("查询客户列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list() {
        log.debug("开始处理【查询客户列表】的请求");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9081/users/login
    @ApiOperation("客户登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO) {
        log.debug("开始处理【客户登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9081/users/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加客户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加客户】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/users/9527/delete
    @ApiOperation("删除客户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "客户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除客户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9081/users
    @ApiOperation("修改前查询")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "Id", value = "服务项目id", required = true, dataType = "long")
    @PostMapping("/selectupdate/{Id:[0-9]+}")
    public JsonResult selectupdate(@PathVariable Long Id) {
        log.debug("开始处理【查询修改客户信息前查询】的请求");
        return JsonResult.ok(userService.selectupdate(Id));
    }

    // http://localhost:9081/users/update/2
    @ApiOperation("修改客户信息")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "id", value = "客户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@Validated UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【修改客户信息】的请求，参数：{}", userAddNewDTO);
        userService.update(userAddNewDTO);
        return JsonResult.ok();
    }

    // "http://localhost:9081/admins/level"
    @ApiOperation("查询客户VIP等级列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/level")
    public JsonResult<List<UserLevelListItemVO>> userlevellist() {
        log.debug("开始处理【查询项目类别列表】的请求");
        List<UserLevelListItemVO> list = userService.userlevellist();
        return JsonResult.ok(list);
    }
}
